<template>
  <view class="wrapForm">
    <view class="bgLine flex jst-between pL25 pR25"> </view>
    <view class="wrapFormBody pL15 pR15 pT15 rzBox">
      <view>
        <text class="font24" v-if="current == 0">企业信息</text>
        <text class="font24" v-if="current == 1">法人信息</text>
        <text class="font24" v-if="current == 2">财税信息</text>
        <text class="pL5 font16">{{ current + 1 }}/3</text>
      </view>
      <!-- 企业信息 -->
      <u-form
        v-show="current == 0"
        :label-style="{ 'font-size': '30rpx' }"
        class="xf-wrap-form"
        ref="enterpriseInformationForm"
        :rules="enterpriseInformationRules"
        :model="formData.enterpriseInformation"
        :error-type="['toast']"
      >
        <u-form-item
          :label-position="'top'"
          label="营业执照图片"
          :required="true"
          prop="businessLicense"
          :border-bottom="false"
        >
          <view class="w100 flex flex-wrap">
            <xfUploadImg
              v-model="formData.enterpriseInformation.businessLicense"
              :limit="9"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="企业名称"
          :label-position="'top'"
          prop="enterpriseName"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入企业名称"
              v-model="formData.enterpriseInformation.enterpriseName"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="统一社会信用代码"
          :label-position="'top'"
          prop="creditCode"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入统一社会信用代码"
              v-model="formData.enterpriseInformation.creditCode"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :label-position="'top'"
          :required="true"
          label="企业类型"
          prop="enterpriseType"
        >
          <view class="w100 font14 inpuTBg"
            ><xfSelect
              class="w100"
              :key="1"
              inputAlign="left"
              :options="enterpriseTypeLis"
              v-model="formData.enterpriseInformation.enterpriseType"
            >
            </xfSelect
          ></view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :label-position="'top'"
          :required="true"
          label="经营范围"
          prop="businessScope"
        >
          <u-input
            :custom-style="{
              background: '#F8F8F8',
              borderRadius: '20rpx',
              padding: '30rpx 30rpx !important',
            }"
            class="textPz w100"
            type="textarea"
            placeholder="请输入经营范围"
            v-model="formData.enterpriseInformation.businessScope"
          />
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="注册资本"
          :label-position="'top'"
          prop="registeredCapital"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入注册资本"
              v-model="formData.enterpriseInformation.registeredCapital"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :label-position="'top'"
          :required="true"
          label="成立日期"
          prop="establishmentDate"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              type="select"
              @click="showEstablishmentDate = true"
              v-model="formData.enterpriseInformation.establishmentDate"
              placeholder="请选择开始日期"
            ></u-input>
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :label-position="'top'"
          :required="true"
          label="营业期限"
          prop="businessTerm"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入营业期限"
              v-model="formData.enterpriseInformation.businessTerm"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="企业地址"
          :label-position="'top'"
          prop="address"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <view class="w70"
              ><u-input
                placeholder="请选择企业地址"
                v-model="formData.enterpriseInformation.address"
              />
            </view>
            <image
              @click="chooseLocation('enterpriseInformation', 'address')"
              class="imgW36"
              :src="resources.posIcon"
              mode=""
            ></image>
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="经办人"
          :label-position="'top'"
          prop="agentName"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入经办人"
              v-model="formData.enterpriseInformation.agentName"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="经办人手机号"
          :label-position="'top'"
          prop="agentPhoneNumber"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入联系电话"
              v-model="formData.enterpriseInformation.agentPhoneNumber"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="经办人邮箱"
          :label-position="'top'"
          prop="agentEmail"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入经办人邮箱"
              v-model="formData.enterpriseInformation.agentEmail"
            />
          </view>
        </u-form-item>
      </u-form>
      <!-- 法人信息 -->
      <u-form
        v-show="current == 1"
        :error-type="['toast']"
        :label-style="{ 'font-size': '30rpx' }"
        class="xf-wrap-form representativeForm"
        ref="representativeForm"
        :rules="representativeRules"
        :model="formData.representative"
      >
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="法人姓名"
          :label-position="'top'"
          prop="realName"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入法人姓名"
              v-model="formData.representative.realName"
            />
          </view>
        </u-form-item>

        <u-form-item
          :required="true"
          :border-bottom="false"
          label="身份证号"
          :label-position="'top'"
          prop="idCardNumber"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入身份证号"
              v-model="formData.representative.idCardNumber"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="发证机关"
          :label-position="'top'"
          prop="signingOrganization"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入发证机关"
              v-model="formData.representative.signingOrganization"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="有效期"
          :label-position="'top'"
          prop="periodValidity"
        >
          <view class="w100 flex font14 inpuTBg">
            <u-input
              class="w100"
              placeholder="请输入有效期"
              v-model="formData.representative.periodValidity"
            />
          </view>
        </u-form-item>
        <view class="flex jst-between" :border-bottom="false">
          <view class="w50 mR10">
            <u-form-item
              :label-position="'top'"
              label="身份证照片"
              :required="true"
              prop="idCardPositive"
              :border-bottom="false"
            >
              <view class="w100 flex flex-wrap">
                <xfUploadImg
                  uploadText="上传头像面"
                  v-model="formData.representative.idCardPositive"
                  :limit="1"
                />
              </view>
            </u-form-item>
          </view>
          <view class="w50 mL10">
            <u-form-item
              class="hidLabel"
              :label-style="{ color: '#fff' }"
              :label-position="'top'"
              :border-bottom="false"
              label="11"
              prop="idCardInstead"
            >
              <view class="w100 flex flex-wrap">
                <xfUploadImg
                  uploadText="上传国徽面"
                  v-model="formData.representative.idCardInstead"
                  :limit="1"
                />
              </view>
            </u-form-item>
          </view>
        </view>
      </u-form>
      <!-- 财税信息 -->
      <u-form
        v-show="current == 2"
        :error-type="['toast']"
        :label-style="{ 'font-size': '30rpx' }"
        class="xf-wrap-form financialInfoForm"
        ref="financialInfoForm"
        :rules="financialInfoRules"
        :model="formData.financialInfo"
      >
        <u-form-item
          :label-position="'top'"
          label="开户许可证照片"
          :required="true"
          prop="permit"
          :border-bottom="false"
        >
          <view class="w100 flex flex-wrap">
            <xfUploadImg v-model="formData.financialInfo.permit" :limit="9" />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :label-position="'top'"
          :required="true"
          label="财税类型"
          prop="financeType"
        >
          <view class="w100 font14 inpuTBg"
            ><xfSelect
              class="w100"
              :key="1"
              inputAlign="left"
              :options="financeTypeLis"
              v-model="formData.financialInfo.financeType"
            >
            </xfSelect
          ></view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="开票明细电话"
          :label-position="'top'"
          prop="telephone"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入开票明细电话"
              v-model="formData.financialInfo.telephone"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="开户行"
          :label-position="'top'"
          prop="bankName"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入开户行"
              v-model="formData.financialInfo.bankName"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="formData.financialInfo.isOrdinary == 1 ? true : false"
          label="普通发票服务费"
          :label-position="'top'"
          :prop="
            formData.financialInfo.isOrdinary == 1
              ? 'ordinaryInvoiceServiceFee'
              : ''
          "
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入普通发票服务费"
              v-model="formData.financialInfo.ordinaryInvoiceServiceFee"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="formData.financialInfo.isSpecial == 1 ? true : false"
          label="专业发票服务费"
          :label-position="'top'"
          :prop="
            formData.financialInfo.isSpecial == 1
              ? 'specialInvoiceServiceFee'
              : ''
          "
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入专业发票服务费"
              v-model="formData.financialInfo.specialInvoiceServiceFee"
            />
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="普票权限"
          :label-position="'top'"
          prop="isOrdinary"
        >
          <view class="w100 flex flex-wrap">
            <view
              :key="'isOrdinary' + i"
              :class="v.checked == true ? 'checked' : ''"
              class="typeItem"
              @click="checkType('financialInfo', 'isOrdinary', v, i)"
              v-for="(v, i) in isOrdinaryLis"
            >
              {{ v.text }}
            </view>
          </view>
        </u-form-item>
        <u-form-item
          :required="true"
          :border-bottom="false"
          label="专票权限"
          :label-position="'top'"
          prop="isSpecial"
        >
          <view class="w100 flex flex-wrap">
            <view
              :key="'isSpecial' + i"
              :class="v.checked == true ? 'checked' : ''"
              class="typeItem"
              @click="checkType('financialInfo', 'isSpecial', v, i)"
              v-for="(v, i) in isSpecialLis"
            >
              {{ v.text }}
            </view>
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="公司对公账户"
          :label-position="'top'"
          prop="cardNumber"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入公司对公账户"
              v-model="formData.financialInfo.cardNumber"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="联系地址"
          :label-position="'top'"
          prop="address"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <view class="w70"
              ><u-input
                placeholder="请选择联系地址"
                v-model="formData.financialInfo.address"
              />
            </view>
            <image
              @click="chooseLocation('financialInfo', 'address')"
              class="imgW36"
              :src="resources.posIcon"
              mode=""
            ></image>
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人"
          :label-position="'top'"
          prop="contacts"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入财务联系人"
              v-model="formData.financialInfo.contacts"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人电话"
          :label-position="'top'"
          prop="contactTelephone"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入财务联系人电话"
              v-model="formData.financialInfo.contactTelephone"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人固定电话"
          :label-position="'top'"
          prop="contactLinePhone"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入财务联系人固定电话"
              v-model="formData.financialInfo.contactLinePhone"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人邮箱"
          :label-position="'top'"
          prop="contactEmail"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入财务联系人邮箱"
              v-model="formData.financialInfo.contactEmail"
            />
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人地址"
          :label-position="'top'"
          prop="contactAddress"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <view class="w70"
              ><u-input
                placeholder="请选择财务联系人地址"
                v-model="formData.financialInfo.contactAddress"
              />
            </view>
            <image
              @click="chooseLocation('financialInfo', 'contactAddress')"
              class="imgW36"
              :src="resources.posIcon"
              mode=""
            ></image>
          </view>
        </u-form-item>
        <u-form-item
          :border-bottom="false"
          :required="true"
          label="财务联系人邮编"
          :label-position="'top'"
          prop="contactPostcode"
        >
          <view class="w100 flex alg-center jst-between inpuTBg">
            <u-input
              placeholder="请输入财务联系人邮编"
              v-model="formData.financialInfo.contactPostcode"
            />
          </view>
        </u-form-item>
      </u-form>
    </view>
    <block v-if="current == 0">
      <view class="flex jst-center mT20 bottomW bgf">
        <view
          class="pointer rightNowPay u-f font15 t-center"
          @click="nextStep(1)"
        >
          下一步
        </view>
      </view>
    </block>
    <block v-else-if="current == 1">
      <view class="flex jst-center mT20 bottomW bgf">
        <view class="returnBack font16 u-blue" @click="backStep(0)">
          返回上一步
        </view>
        <view class="publishBtn font16 u-f mL15" @click="nextStep(2)">
          下一步
        </view>
      </view>
    </block>
    <block v-else>
      <view v-if="!isItCertified" class="flex jst-center mT20 bottomW bgf">
        <view class="returnBack font16 u-blue" @click="backStep(1)">
          返回上一步
        </view>
        <view class="publishBtn font16 u-f mL15" @click="certification">
          认证
        </view>
      </view>
      <view class="flex jst-end mT20 bottomW bgf" v-else>
        <view class="publishBtn font16 u-f mR15" @click="certification">
          认证
        </view>
      </view>
    </block>
    <!-- 成立日期 -->
    <u-picker
      :startYear="1900"
      :default-time="formData.enterpriseInformation.establishmentDate"
      :params="{ year: true, month: true, day: true }"
      v-model="showEstablishmentDate"
      @confirm="setEstablishmentDate"
    ></u-picker>
    <!-- 营业期限 -->
    <!-- <u-picker
      :startYear="1900"
      :default-time="formData.enterpriseInformation.businessTerm"
      :params="{ year: true, month: true, day: true }"
      v-model="showBusinessTerm"
      @confirm="setBusinessTerm"
    ></u-picker> -->
    <!-- 有效期开始日期 -->
    <!-- <u-picker
      :startYear="1900"
      :default-time="formData.enterpriseInformation.establishmentDate"
      :params="{ year: true, month: true, day: true }"
      v-model="showPeriodValidityStart"
      @confirm="setPeriodValidityStart"
    ></u-picker> -->
    <!-- 有效期结束日期 -->
    <!-- <u-picker
      :startYear="1900"
      :default-time="formData.enterpriseInformation.businessTerm"
      :params="{ year: true, month: true, day: true }"
      v-model="showPeriodValidityEnd"
      @confirm="setPeriodValidityEnd"
    ></u-picker> -->
    <xfTipModal ref="tipModal"></xfTipModal>
  </view>
</template>

<script>
import { chooseLocal } from "@/libs/map.js";
import {
  addEnterprise,
  updateEnterpriseInfo,
  findEnterpriseInfoById,
} from "@/api/companyInterface/user.js";
import selAreaMinxs from "@/utils/selArea.js";
export default {
  mixins: [selAreaMinxs],
  components: {},
  data() {
    return {
      companyId: "",
      isItCertified: false,
      current: 0,
      showEstablishmentDate: false,
      showBusinessTerm: false,
      showPeriodValidityEnd: false,
      showPeriodValidityStart: false,
      formData: {
        // 企业信息
        enterpriseInformation: {
          // 营业执照图片
          businessLicense: "",
          // 企业名称
          enterpriseName: "",
          // 统一社会信用代码
          creditCode: "",
          // 企业类型
          enterpriseType: "",
          // 经营范围
          businessScope: "",
          // 注册资本
          registeredCapital: "",
          // 成立日期
          establishmentDate: "",
          // 营业期限
          businessTerm: "",
          // 企业地址
          address: "",
          // 经办人
          agentName: "",
          // 经办人手机号
          agentPhoneNumber: "",
          // 经办人邮箱
          agentEmail: "",
        },
        // 法人信息
        representative: {
          realName: "",
          idCardNumber: "",
          signingOrganization: "",
          periodValidity: "",
          idCardPositive: "",
          idCardInstead: "",
        },
        // 财税信息
        financialInfo: {
          permit: "",
          financeType: "",
          telephone: "",
          bankName: "",
          cardNumber: "",
          address: "",
          contacts: "",
          contactTelephone: "",
          contactLinePhone: "",
          contactEmail: "",
          contactAddress: "",
          contactPostcode: "",
          ordinaryInvoiceServiceFee: "",
          specialInvoiceServiceFee: "",
          isOrdinary: "",
          isSpecial: "",
        },
      },
      // 企业信息
      enterpriseInformationRules: {
        businessLicense: [
          {
            required: true,
            message: "营业执照图片不能为空",
          },
        ],
        enterpriseName: [
          {
            required: true,
            message: "企业名称不能为空",
            trigger: ["change", "blur"],
          },
        ],
        creditCode: [
          {
            required: true,
            message: "统一社会信用代码不能为空",
            trigger: ["change", "blur"],
          },
        ],
        enterpriseType: [
          {
            required: true,
            message: "企业类型不能为空",
            trigger: ["change", "blur"],
          },
        ],
        businessScope: [
          {
            required: true,
            message: "经营范围不能为空",
            trigger: ["change", "blur"],
          },
        ],
        registeredCapital: [
          {
            required: true,
            message: "注册资本不能为空",
            trigger: ["change", "blur"],
          },
        ],
        establishmentDate: [
          {
            required: true,
            message: "成立日期不能为空",
            trigger: ["change", "blur"],
          },
        ],
        businessTerm: [
          {
            required: true,
            message: "营业期限不能为空",
            trigger: ["change", "blur"],
          },
        ],
        address: [
          {
            required: true,
            message: "企业地址不能为空",
            trigger: ["change", "blur"],
          },
        ],
        agentName: [
          {
            required: true,
            message: "经办人不能为空",
            trigger: ["change", "blur"],
          },
        ],
        agentPhoneNumber: [
          {
            required: true,
            message: "经办人手机号不能为空",
            trigger: ["change", "blur"],
          },
        ],
        agentEmail: [
          {
            required: true,
            message: "经办人邮箱不能为空",
            trigger: ["change", "blur"],
          },
        ],
      },
      // 法人信息
      representativeRules: {
        realName: [
          {
            required: true,
            message: "法人姓名不能为空",
            trigger: ["change", "blur"],
          },
        ],
        idCardNumber: [
          {
            required: true,
            message: "身份证号不能为空",
            trigger: ["change", "blur"],
          },
          {
            rule: /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/,
            msg: "请正确输入您的身份证号码15-18位",
          },
        ],
        signingOrganization: [
          {
            required: true,
            message: "签发机关不能为空",
            trigger: ["change", "blur"],
          },
        ],
        periodValidity: [
          {
            required: true,
            message: "有效期不能为空",
            trigger: ["change", "blur"],
          },
        ],
        idCardPositive: [
          {
            required: true,
            message: "身份证正面照片不能为空",
          },
        ],
        idCardInstead: [
          {
            required: true,
            message: "身份证反面照片不能为空",
          },
        ],
      },
      // 财税信息
      financialInfoRules: {
        permit: [
          {
            required: true,
            message: "开户许可证照片不能为空",
          },
        ],
        financeType: [
          {
            required: true,
            message: "财税类型不能为空",
            trigger: ["change", "blur"],
          },
        ],
        telephone: [
          {
            required: true,
            message: "开票明细电话不能为空",
            trigger: ["change", "blur"],
          },
        ],
        bankName: [
          {
            required: true,
            message: "开户行不能为空",
            trigger: ["change", "blur"],
          },
        ],
        cardNumber: [
          {
            required: true,
            message: "企业对公账户不能为空",
            trigger: ["change", "blur"],
          },
        ],
        address: [
          {
            required: true,
            message: "联系地址不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contacts: [
          {
            required: true,
            message: "财务联系人不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contactTelephone: [
          {
            required: true,
            message: "财务联系人电话不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contactLinePhone: [
          {
            required: true,
            message: "财务联系人固定电话不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contactEmail: [
          {
            required: true,
            message: "财务联系人邮箱不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contactAddress: [
          {
            required: true,
            message: "财务联系人地址不能为空",
            trigger: ["change", "blur"],
          },
        ],
        contactPostcode: [
          {
            required: true,
            message: "财务联系人邮编不能为空",
            trigger: ["change", "blur"],
          },
        ],
        ordinaryInvoiceServiceFee: [
          {
            required: true,
            message: "普通发票服务费不能为空",
            trigger: ["change", "blur"],
          },
        ],
        specialInvoiceServiceFee: [
          {
            required: true,
            message: "专业发票服务费不能为空",
            trigger: ["change", "blur"],
          },
        ],
        isOrdinary: [
          {
            required: true,
            message: "请选择普票权限",
            trigger: ["change", "blur"],
          },
        ],
        isSpecial: [
          {
            required: true,
            message: "请选择专票权限",
            trigger: ["change", "blur"],
          },
        ],
      },
      // 企业类型列表
      enterpriseTypeLis: [],
      // 经营范围列表
      businessScopeLis: [],
      // 财税类型
      financeTypeLis: [],
      // 普票
      isOrdinaryLis: [],
      // 专票
      isSpecialLis: [],
    };
  },
  onReady() {
    this.$refs.enterpriseInformationForm.setRules(
      this.enterpriseInformationRules
    );
    this.$refs.representativeForm.setRules(this.representativeRules);
    this.$refs.financialInfoForm.setRules(this.financialInfoRules);
  },
  onShow() {},
  async onLoad(e) {
    this.companyId = e.companyId;
    this.setDefaultTime();
    await this.initDic();
    this.getInfo();
  },
  methods: {
    checkZzfw(e, i) {
      console.log(this.zzfwLis);
    },
    focus() {
      this.formData.oddJobUseNum = "";
      this.isFocus = true;
      for (let i = 0; i < this.oddJobUseNumLis.length; i++) {
        this.oddJobUseNumLis[i].checked = false;
      }
    },
    inputNumber(e) {
      this.formData.oddJobUseNum = e;
    },
    initDic() {
      let arr = [
        // 企业类型
        {
          name: "jjp_oddjob_settlement_enumeration_type",
          v: "enterpriseTypeLis",
          current: "enterpriseInformation",
          text: "enterpriseType",
        },
        // 财税类型
        {
          name: "jjp_oddjob_settlement_financial_info",
          v: "financeTypeLis",
          current: "financialInfo",
          text: "financeType",
        },
        // 普票权限
        {
          name: "jjp_oddjob_settlement_whether",
          v: "isOrdinaryLis",
          current: "financialInfo",
          text: "isOrdinary",
        },
        // 专票权限
        {
          name: "jjp_oddjob_settlement_whether",
          v: "isSpecialLis",
          current: "financialInfo",
          text: "isSpecial",
        },
      ];

      for (let i = 0; i < arr.length; i++) {
        this.baseStore.getDictionaryDataAll(arr[i].name).then((res) => {
          this[arr[i].v] = res.map((v, j) => {
            if (j == 0) {
              const current = arr[i].current;
              this.formData[current][arr[i].text] = v.value;
              this.formData[current][arr[i].text + "Text"] = v.text;
            }
            return {
              ...v,
              checked: j == 0 ? true : false,
            };
          });
        });
      }
    },
    setDefaultTime() {
      let time = new Date();
      let d = this.xf.toDate(time, "yyyy-MM-dd");
      //   this.formData.enterpriseInformation.establishmentDate = d;
      //   this.formData.enterpriseInformation.businessTerm = d;
    },
    convertUrlsToObjects(formData, fieldMapping) {
      fieldMapping.forEach(({ formKey, fields }) => {
        fields.forEach((field) => {
          const value = formData[formKey][field];
          if (value && value.length > 0) {
            formData[formKey][field] = value.split(",").map((url) => ({ url }));
          }
        });
      });
    },
    getInfo() {
      findEnterpriseInfoById({}).then((res) => {
        if (res) {
          const {
            isItCertified = false,
            enterpriseInformation = {},
            representative = {},
            financialInfo = {},
          } = res.result;
          // 未认证直接返回
          this.isItCertified = isItCertified;
          if (!isItCertified) return;
          this.current = 2;
          let businessLicenseArr = [];
          let idCardPositiveArr = [];
          let idCardInsteadArr = [];
          let permitArr = [];
          // 营业执照图片
          if (enterpriseInformation.businessLicense) {
            let d = enterpriseInformation.businessLicense.split(",");
            businessLicenseArr = d.map((v) => {
              return {
                url: v,
              };
            });
          }
          // 身份证正面照片
          if (representative.idCardPositive) {
            let d = representative.idCardPositive.split(",");
            idCardPositiveArr = d.map((v) => {
              return {
                url: v,
              };
            });
          }
          // 身份证反面照片
          if (representative.idCardInstead) {
            let d = representative.idCardInstead.split(",");
            idCardInsteadArr = d.map((v) => {
              return {
                url: v,
              };
            });
          }
          // 开户许可证照片
          if (financialInfo.permit) {
            let d = financialInfo.permit.split(",");
            permitArr = d.map((v) => {
              return {
                url: v,
              };
            });
          }

          this.formData = {
            enterpriseInformation: {
              ...enterpriseInformation,
              businessLicense: businessLicenseArr,
            },
            representative: {
              ...representative,
              idCardPositive: idCardPositiveArr,
              idCardInstead: idCardInsteadArr,
            },
            financialInfo: {
              ...financialInfo,
              permit: permitArr,
            },
          };
        }
      });
    },
    // 成立日期
    setEstablishmentDate(e) {
      this.formData.enterpriseInformation.establishmentDate =
        e.year + "-" + e.month + "-" + e.day;
    },
    // 营业期限
    setBusinessTerm(e) {
      this.formData.enterpriseInformation.businessTerm =
        e.year + "-" + e.month + "-" + e.day;
    },
    // 有效期开始时间
    setPeriodValidityStart(e) {
      this.formData.representative.periodValidityStart =
        e.year + "-" + e.month + "-" + e.day;
    },
    // 有效期结束时间
    setPeriodValidityEnd(e) {
      this.formData.representative.periodValidityEnd =
        e.year + "-" + e.month + "-" + e.day;
    },
    // 下一步
    nextStep(current) {
      if (current == 1) {
        this.$refs.enterpriseInformationForm.validate((valid) => {
          if (valid) {
            this.current = current;
          }
        });
      } else if (current == 2) {
        this.$refs.representativeForm.validate((valid) => {
          if (valid) {
            this.current = current;
          }
        });
      }
    },
    // 返回上一步
    backStep(current) {
      this.current = current;
    },
    //选择位置信息
    async chooseLocation(current, name) {
      let res = await chooseLocal();
      let r = res.address + res.name;
      let d = this.regionDiscern(r);
      this.formData[current][name] = d.data.formatted_address;
    },
    checkType(current, name, v, i) {
      this.formData[current][name] = v.value;
      this.formData[current][name + "Text"] = v.text;
      this[name + "Lis"].map((item, index) => {
        item.checked = index === i ? true : false;
      });
    },
    fieldsToConvertImg(data) {
      // 定义字段映射
      const fieldsToConvert = [
        { formKey: "enterpriseInformation", fields: ["businessLicense"] },
        {
          formKey: "representative",
          fields: ["idCardPositive", "idCardInstead"],
        },
        { formKey: "financialInfo", fields: ["permit"] },
      ];
      // 遍历字段映射并转换
      fieldsToConvert.forEach(({ formKey, fields }) => {
        fields.forEach((field) => {
          if (
            Array.isArray(data[formKey][field]) && // 检查是否为数组
            data[formKey][field].length > 0
          ) {
            data[formKey][field] = data[formKey][field]
              .map((obj) => obj.url)
              .join(",");
          }
        });
      });
    },
    certification() {
      this.$refs.financialInfoForm.validate((valid) => {
        if (valid) {
          let data = this.xf.deepClone(this.formData);
          this.fieldsToConvertImg(data);
          // 编辑
          if (this.isItCertified) {
            const { financialInfo = {} } = this.formData;
            updateEnterpriseInfo({
              enterpriseId: financialInfo.enterpriseId,
              contacts: financialInfo.contacts,
              contactTelephone: financialInfo.contactTelephone,
              contactLinePhone: financialInfo.contactLinePhone,
              contactAddress: financialInfo.contactAddress,
              contactPostcode: financialInfo.contactPostcode,
            }).then((res) => {
              this.xf.onlineUtils.toast("修改成功");
              setTimeout(() => {
                uni.navigateBack({
                  delta: 1,
                });
              }, 2000);
            });
          } else {
            addEnterprise({ ...data }).then(
              (res) => {
                this.xf.onlineUtils.toast("认证成功");
                setTimeout(() => {
                  uni.navigateBack({
                    delta: 1,
                  });
                }, 2000);
              }
            );
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
page {
  min-height: 100%;
  background-color: #fff;
}

.inpuTBg .uicon-arrow-right {
  content: "\e600" !important;
}
</style>
<style lang="scss" scoped>
.inpuTBg {
  padding: 0 30rpx;
  height: 90rpx;
  background: #f8f8f8;
  border-radius: 20rpx 20rpx 20rpx 20rpx;
  display: flex;
  align-items: center;
}

.salaryInfo {
  color: #ff9c5a;
  font-size: 24rpx;
  margin-top: 24rpx;
  line-height: 43rpx;
}
.typeItem {
  width: calc(33% - 30rpx);
  height: 80rpx;
  line-height: 80rpx;
  border: 2rpx solid transparent;
  text-align: center;
  background: #f8f8f8;
  font-size: 28rpx;
  margin-bottom: 20rpx;
  border-radius: 20rpx;
  margin-right: 20rpx;

  &:nth-child(3n) {
    margin-right: 0;
  }

  &.checked {
    background: #eaf4fe;
    border-radius: 16rpx;

    /* #ifdef MP-COMPANY */
    color: #3291f8;
    border: 2px solid #3291f8;
    /* #endif */
    /* #ifdef MP-ECONOMIC */
    color: #00a0c6;
    border: 2px solid #00a0c6;

    /* #endif */
  }
  &.checked-disabled {
    background: #f8f8f8;
    border-radius: 16rpx;
    color: #999999;
    border: 1px solid #999999;
  }
}

.setalle {
  padding-top: 70rpx;
}

.wrapForm {
  .u-form-item {
    padding: 0 !important;
  }

  .rzCardBox {
    height: 128rpx;
    background: #f8f8f8;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    margin-top: 60rpx;

    &:last-child {
      margin-top: 30rpx;
    }

    .imgRzCard {
      width: 40rpx;
      height: 32rpx;
    }
  }

  padding-bottom: 240rpx;

  // height: 100%;

  .wrapFormBody {
    margin-top: -30rpx;
    border-radius: 40rpx 40rpx 0 0;
    background: #fff;
  }

  .uni-forms-item {
    align-items: center !important;
  }

  .uni-input-placeholder {
    // text-align: right !important;
    color: #bdbdbd !important;
  }
}

.bgLine {
  /* #ifdef MP-COMPANY */
  background-color: #ebf0ff;
  /* #endif */
  /* #ifdef MP-ECONOMIC */
  background-color: #ebfcff;
  /* #endif */

  width: 100%;
  // height: 60rpx;
  padding: 30rpx 40rpx;

  .rzImg {
    width: 168rpx;
    height: 192rpx;
  }
}

.rightNowPay {
  width: 670rpx;
  height: 100rpx;
  line-height: 100rpx;

  border-radius: 30rpx;
  /* #ifdef MP-COMPANY */
  background: #3291f8;
  /* #endif */
  /* #ifdef MP-ECONOMIC */
  background: #00a0c6;
  /* #endif */
}
</style>
